<?php

class App_Model_DbTable_Reply extends Ext_Db_Table  
{  
    protected $_name = 'reply';
    
    private static $_reply = array();
        
    public function getRowById($articleId)
    {
        if(!$articleId)
        {
            return "";
        }
        
        if(isset(self::$_reply['articleId'][$articleId]))
        {
            return self::$_reply['articleId'][$articleId];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" articleId = ? ", $articleId);
        $row = $this->fetchRow($safe);
        if (!$row) {
            return "";
        }
        $reply = $row->toArray();

        self::$_reply['articleId'][$articleId] = $reply;
        
        return $reply;
    }
    
    public function del($articleId)
    {
        if(!$articleId)
        {
            return "";
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" articleId = ? ", $articleId);
        $rows_affected = $this->delete($safe);
        if($rows_affected){
            $this->_unsetReply($articleId);
        }
        return "";
    }

    public function addNew($inputArray)
    {
        $sql_data_array = array();
        if(array_key_exists('userId', $inputArray)) $sql_data_array['userId'] = $inputArray['userId'];
        if(array_key_exists('articleId', $inputArray)) $sql_data_array['articleId'] = $inputArray['articleId'];
        if(array_key_exists('destArticleId', $inputArray)) $sql_data_array['destArticleId'] = $inputArray['destArticleId'];
        if(!$sql_data_array)
        {
            return "";
        }
        
        try {
            $rows_affected = $this->insert($sql_data_array);
            $last_insert_id = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        return $last_insert_id;
    }
    
    private function _unsetReply($articleId)
    {
        if(!$articleId)
        {
            return "";
        }
        
        $reply = $this->getRowById($articleId);
        if(!$reply)
        {
            return "";
        }
        unset(self::$_reply['articleId'][$articleId]);
    }
}  

